/*
	Copyright 2008, 2009, 2010 Brightworks, Inc.
	
	This file is part of Language Mentor.
	
	Language Mentor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Language Mentor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.controller
{
    import com.brightworks.util.Debug;
    import com.langcollab.languagementor.constant.Constant_Misc;
    import com.langcollab.languagementor.controller.download.DownloadProcessManager;
    import com.langcollab.languagementor.controller.download.DownloadProcessManagerResultsReport;
    import com.langcollab.languagementor.event.Event_Download;
    import com.langcollab.languagementor.model.Callbacks;
    import com.langcollab.languagementor.model.MainModel;
    
    import flash.events.Event;
    import flash.filesystem.File;
    
    import org.swizframework.utils.commands.IEventAwareCommand;

    public class Command_DownloadModules extends Command_Base implements IEventAwareCommand
    {
		[Inject]
		public var model:MainModel;

		private var _downloadManager:DownloadProcessManager;

        // --------------------------------------------
        //
        //
        //           Public Methods
        //
        //
        // --------------------------------------------

        public function Command_DownloadModules()
        {
            super();
        }

		public function execute():void
        {
			Debug.info("Command_DownloadModules.execute() - create/start dnld process mgr");
            var file:File = File.applicationStorageDirectory;
            file = file.resolvePath(Constant_Misc.FILEPATHINFO_DOWNLOADED_MODULES_FOLDER_NAME + File.separator + Event_Download(event).contentProviderId);
            var saveRoot:String = file.nativePath;
            _downloadManager = new DownloadProcessManager(model);
            _downloadManager.downloadURLRoot               = model.currentRepositoryRootURL;
            _downloadManager.downloadFileExtension         = Constant_Misc.DOWNLOADPROCESS_DOWNLOAD_FILE_EXTENSION;
            _downloadManager.saveFileFolderRoot            = saveRoot;
            _downloadManager.publishedModuleVersionIdList  = Event_Download(event).publishedModuleVersionIdList;
            var callbacks:Callbacks = new Callbacks(result, result);
            _downloadManager.start(callbacks);
        }

		override public function result(data:Object = null):void
		{
			Debug.info("Command_DownloadModules.result() - this could mean failure, partial success, or complete success ");
			var report:DownloadProcessManagerResultsReport = DownloadProcessManagerResultsReport(data);
			var userFriendlyMessage:String = "Download process finished. " + report.downloadProcessCount_Succeeded + "/" + report.downloadProcessCount_Started + " downloads were successful."; 
			Debug.provideFeedbackToUser_UserFriendly(userFriendlyMessage, report);			
			super.result();
		}
		
    }
}
